package com.antrain.utils;

import java.io.File;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.antrain.annotation.Colunm;
import com.antrain.annotation.Id;
import com.antrain.annotation.Table;

/**
 * 利用反射来解析实体类中注解的类
 */
@SuppressWarnings("all")
public class AnnotationUtil {

    /**
     * 得到类名
     * 
     * @param clz 实体类
     * @return
     */
    public static String getClassName(Class clz) {
        return clz.getName();
    }

    /**
     * 获取Table注解上的表名
     * 
     * @param clz
     * @return
     */
    public static String getTableName(Class clz) {
        if (clz.isAnnotationPresent(Table.class)) {
            Table table = (Table) clz.getAnnotation(Table.class);
            return table.name();
        } else {
            throw new RuntimeException("缺少Table注解");
        }
    }

    /**
     * 获取主键的列名
     * @param clz
     * @return
     */
    public static Map<String,String> getIdMapper(Class clz) {
        boolean flag = true;
        Map<String,String> map = new HashMap<String,String>();
        Field[] fields = clz.getDeclaredFields();
        for (Field field : fields){
            if (field.isAnnotationPresent(Id.class)){
                flag = false;
                String fieldName = field.getName();
                if (field.isAnnotationPresent(Colunm.class)){
                    Colunm colunm = field.getAnnotation(Colunm.class);
                    String colunmName = colunm.name();
                    map.put(fieldName,colunmName);
                    break;
                }else{
                    throw new RuntimeException("Id上缺少Column注解");
                }
            }
        }
        if (flag) {
            throw new RuntimeException("缺少Id注解");
        }
        return map;
    }
    
    /**
     * 获取实体类中所有属性和相应的字段
     * @param clz
     * @return
     */
    public static Map<String,String> getPropMapping(Class clz) {
        Map<String,String> map = new HashMap<String,String>();
        map.putAll(getIdMapper(clz));
        Field[] fields = clz.getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(Colunm.class)){
                Colunm colunm = field.getAnnotation(Colunm.class);
                String fieldName = field.getName();
                String columnName = colunm.name();
                map.put(fieldName, columnName);
            }
        }
        return map;
    }

    public static Set<String> getClassNameByPacking(String packagePath) {
        Set<String> names = new HashSet<String>();
        String packageFile = packagePath.replace(".", "/");
        String classpath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
        if(classpath == null){
            classpath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
        }
        try {
            classpath = java.net.URLDecoder.decode(classpath,"utf-8");  
        } catch (Exception e) {
           e.printStackTrace();
        }
        File dir = new File(classpath+packageFile);
        if(dir.exists()){
            File[] files = dir.listFiles();
            for (File f : files){
                String name = f.getName();
                if (f.isFile() && name.endsWith(".class")){
                    name = packagePath+"."+name.substring(0,name.lastIndexOf("."));
                    names.add(name);
                }
            }
        }else{
            throw new RuntimeException("包路径不存在");
        }
        return names;
    }

}